package com.wei.groupingcomparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class OrderInfo implements WritableComparable<OrderInfo> {
	private String id;
	private String pid;
	private double price;

	public OrderInfo() {
	}

	public void set(String id, String pid, double price) {
		this.id = id;
		this.pid = pid;
		this.price = price;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "OrderInfo [id=" + id + ", pid=" + pid + ", price=" + price
				+ "]";
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(id);
		out.writeUTF(pid);
		out.writeDouble(price);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.id = in.readUTF();
		this.pid = in.readUTF();
		this.price = in.readDouble();
	}

	@Override
	public int compareTo(OrderInfo o) {
		int compareTo = this.id.compareTo(o.getId());
		if (compareTo == 0) {
			compareTo = this.price > o.getPrice() ? 1 : -1;
		}
		return compareTo;
	}

}
